package com.example.mybus;

import java.io.IOException;
import java.util.logging.Logger;

import javax.jdo.PersistenceManager;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.example.mybus.dao.MicroDao;
import com.example.mybus.dao.ViajeDao;
import com.example.mybus.model.Micro;
import com.example.mybus.model.Viaje;
import com.example.mybus.model.pmf.PMF;

public class CrearViajeServlet extends HttpServlet {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 5982728608941031557L;
	private static final Logger log = Logger.getLogger("CrearViajeServlet");

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		
		doPost(req, resp);
	}
	
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		
		String patente = req.getParameter("patente");
		String origen = req.getParameter("origen");
		String destino = req.getParameter("destino");
		String salida = req.getParameter("salida");
		String llegada = req.getParameter("llegada");
		
		PersistenceManager pm = PMF.get().getPersistenceManager();
		
		Micro micro = MicroDao.getMicroByPatente(patente, pm);
		Viaje viaje = new Viaje(origen, destino, micro);
		viaje.setFechaSalida(salida);
		viaje.setFechaLlegada(llegada);
		
		ViajeDao.guardarViaje(viaje);
		
		req.getRequestDispatcher("viajes").forward(req, resp);
	}
	
}
